<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
      <el-steps :active="active" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="地址信息"></el-step>
      </el-steps>
      <br />
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <div v-show="active==1" style="margin-top: 20px;">
            <el-col :span="12">
              <el-form-item label="委托人" prop="client.id" :rules="[
                  {required: true, message:'委托人ID不能为空', trigger:'blur'}
                 ]">
                <GridSelect title="选择委托人ID" labelName='nikeName' labelValue='id' :value="inputForm.client.id" :limit="1"
                  @getValue='(value) => {inputForm.client.id=value}' :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'phone',
              label: '手机号(账号唯一)'
            },
            {
              prop: 'nikeName',
              label: '昵称'
            }
            ]" :searchs="[
            {
              prop: 'phone',
              label: '手机号(账号唯一)'
            },
            {
              prop: 'nikeName',
              label: '昵称'
            }
            ]" dataListUrl="/userinfo/tUserInfo/list" entityBeanName="tUserInfo"
                  queryEntityUrl="/userinfo/tUserInfo/queryById">
                </GridSelect>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="业务类型" prop="businessType" :rules="[
                {required: true, message:'业务类型不能为空', trigger:'blur'}
               ]">
                <el-select v-model="inputForm.businessType" placeholder="请选择业务类型" style="width: 100%;">
                  <el-option v-for="item in $dictUtils.getDictList('business_type')" :key="item.value"
                    :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="委托类型" prop="delegationType" :rules="[
                 ]">
                <el-select v-model="inputForm.delegationType" placeholder="请选择委托类型" style="width: 100%;">
                  <el-option label="住宅" value="1">住宅</el-option>
                  <el-option label="商铺" value="2">商铺</el-option>
                  <el-option label="写字楼" value="3">写字楼</el-option>
                  <el-option label="厂房" value="4">厂房</el-option>
                  <el-option label="土地" value="5">土地</el-option>
                  <el-option label="车位" value="6">车位</el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="房源类型" prop="secondaryType" :rules="[
                 ]">
                <el-select v-model="inputForm.secondaryType" placeholder="请选择房源类型" style="width: 100%;">
                  <el-option label="出租房" value="1">出租房</el-option>
                  <el-option label="新房" value="2">新房</el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系方式" prop="contactInformation" :rules="[
                 ]">
                <el-input v-model="inputForm.contactInformation" placeholder="请填写联系方式"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否撤销" prop="whetherRevoke" :rules="[
                 ]">
                <el-select v-model="inputForm.whetherRevoke" placeholder="请选择" style="width: 100%;">
                  <el-option label="否" value="0">否</el-option>
                  <el-option label="是" value="1">是</el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注信息" prop="remarks" :rules="[
                 ]">
                <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
              </el-form-item>
            </el-col>
          </div>
          <div v-show="active==2" style="margin-top: 20px;">
            <el-col :span="12">
              <el-form-item label="住宅/车辆 位置" prop="position" :rules="[
                 ]">
                <el-input v-model="inputForm.position" ref="handleinput" id="handleinput" placeholder="请填写住宅/车辆 位置">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="经度" prop="longitude" :rules="[
                 ]">
                <el-input v-model="inputForm.longitude" placeholder="请填写经度"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="纬度" prop="latitude" :rules="[
                 ]">
                <el-input v-model="inputForm.latitude" placeholder="请填写纬度"></el-input>
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="地图" prop="">
                <div ref="container" id="containepic"></div>
              </el-form-item>
            </el-col>
          </div>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button v-if="active !== 1" style="margin-top: 12px;" @click="upper">上一步</el-button>
        <el-button v-if="active !== 2" style="margin-top: 12px;" @click="next">下一步</el-button>

        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data() {
      return {
        active: 1,
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          businessType: '',
          client: {
            id: ''
          },
          delegationType: '',
          secondaryType: '',
          position: '',
          longitude: '',
          latitude: '',
          contactInformation: '',
          whetherRevoke: '',
          remarks: ''
        },
        markersArray: [],
        url: "",
        urlData: [],
        radio: "2",
        valueList: [],


      }
    },
    components: {
      GridSelect
    },
    methods: {
      init(method, id) {
        this.active = 1;
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建委托租售`
        } else if (method === 'edit') {
          this.title = '修改委托租售'
        } else if (method === 'view') {
          this.title = '查看委托租售'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/commondelegationsale/commonDelegationSale/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({ data }) => {
              this.initMap(this.inputForm.longitude, this.inputForm.latitude);
              this.inputForm = this.recover(this.inputForm, data.commonDelegationSale)
              this.loading = false
            })
          } else {
            this.initMap();
          }
        })
      },
      // 表单提交
      doSubmit() {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/commondelegationsale/commonDelegationSale/save`,
              method: 'post',
              data: this.inputForm
            }).then(({ data }) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      },
      next() {
        this.active++;
        if (this.active == 1) {
          console.log(123213213213);
          // this.initMap();
        }
      },
      upper() {
        this.active--;
        if (this.active == 1) {
          console.log(123213213213);
          // this.initMap();
        }
      },
      initMap(longitude, latitude) {
        var that = this;
        let address = "";
        let lat = "36.752508224779746";
        let logt = "119.97121810913086";
        if (!longitude) {
          let lat = latitude;
          let logt = longitude;
        }
        if (this.inputForm.latitude === "") {
          var center = new qq.maps.LatLng(lat, logt);
        } else {
          var center = new qq.maps.LatLng(
            this.inputForm.latitude,
            this.inputForm.longitude
          );
        }
        var map = new qq.maps.Map(document.getElementById("containepic"), {
          center: center,
          zoom: 13,
          disableDefaultUI: true,
        });
        var marker = new qq.maps.Marker({
          position: center,
          map: map,
        });
        that.markersArray.push(marker);
        var infoWin = new qq.maps.InfoWindow({
          map: map,
        }); //此处是点击地图后生成的地标名称
        var geocoder = new qq.maps.Geocoder({
          complete: function (res) {
            that.deleteOverlays();
            map.setCenter(res.detail.location);
            var marker = new qq.maps.Marker({
              map: map,
              position: res.detail.location,
            });
            that.markersArray.push(marker);
          },
        });

        qq.maps.event.addListener(map, "click", function (event) {
          that.deleteOverlays();
          // this.latitude = event.latLng.getLat();
          // this.longiistude = event.latLng.getLng();
          // console.log(this.longiistude, this.latitude);
          that.inputForm.longitude = event.latLng.getLng();
          that.inputForm.latitude = event.latLng.getLat();
          var position = new qq.maps.LatLng(
            that.inputForm.latitude,
            that.inputForm.longitude
          );
          var marker = new qq.maps.Marker({
            map: map,
            position: position,
          });
          that.markersArray.push(marker);
        });
        document
          .getElementById("handleinput")
          .addEventListener("blur", function () {
            console.log("1111");
            geocoder.getLocation(that.inputForm.position);
          });
      },
      deleteOverlays() {
        if (this.markersArray) {
          for (var i in this.markersArray) {
            this.markersArray[i].setMap(null);
          }
          this.markersArray.length = 0;
        }
      },

    }
  }
</script>

<style lang="less">
  #containepic {
    width: 750px;
    height: 500px;
  }

  .showPic .el-message-box__message {
    width: 800px;
    height: 800px;
    overflow: hidden;
    display: flex;

    p img {
      height: 100% !important;
      width: 100% !important;
      object-fit: contain;
    }
  }

  .imgstyle {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }
</style>